package com.hskn.hss.module.gridBindCheckWork.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hskn.hss.core.utils.StringUtils;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.grid.To.GridTo;
import com.hskn.hss.module.gridBindCheckWork.entity.GridBindCheckwork;
import com.hskn.hss.module.gridBindCheckWork.mapper.GridBindCheckWorkMapper;
import com.hskn.hss.module.gridBindCheckWork.service.GridBindCheckWorkService;
import com.hskn.hss.module.gridCheckWorkSnapshot.to.GridBindCheckworkTo;
import com.hskn.hss.utils.GroupingByUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hskn
 * @since 2021-05-08
 */
@Service
public class GridBindCheckWorkServiceImpl extends ServiceImpl<GridBindCheckWorkMapper, GridBindCheckwork> implements GridBindCheckWorkService {

    @Resource
    private GridBindCheckWorkMapper gridBindCheckWorkMapper;


    @Override
    public List<String> gridIdSelect(GridBindCheckworkTo gridBindCheckworkTo) throws HssException, ParseException {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<String> gridIds = new ArrayList<>();

        Date startTime = sdf.parse(gridBindCheckworkTo.getStartTime());
        Date endTime = sdf.parse(gridBindCheckworkTo.getEndTime());
        QueryWrapper<GridBindCheckwork> gridBindCheckworkQueryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(gridBindCheckworkTo.getItemId())){
            gridBindCheckworkQueryWrapper.lambda().eq(GridBindCheckwork::getItemId,gridBindCheckworkTo.getItemId());
        }
        if(StringUtils.isNotEmpty(gridBindCheckworkTo.getStartTime())){
            gridBindCheckworkQueryWrapper.lambda().ge(GridBindCheckwork::getStartTime,startTime);
        }
        if(StringUtils.isNotEmpty(gridBindCheckworkTo.getEndTime())){
            gridBindCheckworkQueryWrapper.lambda().le(GridBindCheckwork::getEndTime,endTime);
        }
        List<GridBindCheckwork> gridBindCheckworkList = gridBindCheckWorkMapper.selectList(gridBindCheckworkQueryWrapper);

        if(gridBindCheckworkList.size()>0 && CollectionUtils.isNotEmpty(gridBindCheckworkList)){
            for (GridBindCheckwork gridBindCheckwork : gridBindCheckworkList) {
                gridIds.add(gridBindCheckwork.getGridId());
            }
            gridIds = gridIds.stream().distinct().collect(Collectors.toList());
        }
        return gridIds;
    }
    @Override
    public Map<String,GridBindCheckwork> getGridBindCheckwork(Wrapper<GridBindCheckwork> wrapper){
        List<GridBindCheckwork> gridBindCheckworkList = list(wrapper);
        Map<String,GridBindCheckwork> gridBindCheckworkMap = new HashMap<>();
        if (CollectionUtils.isNotEmpty(gridBindCheckworkList)) {
            gridBindCheckworkMap = GroupingByUtils.getGroupingForMapEntity(gridBindCheckworkList, GridBindCheckwork::getId, Collectors.collectingAndThen(Collectors.toList(), value -> value.get(0)));
        }
        return gridBindCheckworkMap;
    }
}
